package com.ebt.m.proposal_v2.widget.base;

import android.content.Context;
import android.util.AttributeSet;

/**
 * 自定义RadioButton的基类
 *
 * @author archer.qi
 *         Created on 2017/5/8.
 */
public abstract class RadioView extends CompatLinearLayout {

    private boolean isChecking;
    private onRadioCheckChangedListener listener;

    public RadioView(Context context) {
        super(context);
    }

    public RadioView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnRadioCheckChangedListener(onRadioCheckChangedListener listener) {
        this.listener = listener;
    }

    /**
     * 仅由基类控制checked标识变换
     *
     * @return
     */
    public final boolean isChecked() {
        return isChecking;
    }

    /**
     * 仅更新check状态
     *
     * @param check
     */
    public final void updateCheckStatus(boolean check) {
        isChecking = check;
    }

    /**
     * 根据check状态更新View显现，由子类自己定义
     *
     * @param check
     */
    public abstract void updateCheck(boolean check);

    /**
     * final方法，不可重写；check变换仅由基类控制，子类只负责更新check状态的直观显现.
     *
     * @param check
     */
    public final void setCheck(boolean check) {
        if (check != isChecking) {
            updateCheck(check);
            if (listener != null) {
                listener.onRadioCheckChanged(getId(), check);
            }
            isChecking = check;
        }
    }

    public interface onRadioCheckChangedListener {
        void onRadioCheckChanged(int resId, boolean checked);
    }

}
